package com.project.demo.config;

import com.project.demo.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 */
@Configuration
@Slf4j
public class WebAppConfig implements WebMvcConfigurer {

    @Value("${file.upload.path:src/main/resources/static/uploads}")
    private String fileUploadPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截的管理器
        InterceptorRegistration registration = registry.addInterceptor(loginInterceptor());
        //拦截的地址
        registration.addPathPatterns("/**");
        //方行的地址
//        registration.excludePathPatterns("/**");
        //根据需要拦截，一般设置所有地址拦截，放行公共连接
        
        // 排除图片路径，允许访问
        registration.excludePathPatterns("/**/*.jpg", "/**/*.png", "/**/*.jpeg", "/**/*.gif");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加资源处理程序，处理已有的以 /api/ 开头的图片URL
        registry.addResourceHandler("/**/*.jpg", "/**/*.png", "/**/*.jpeg", "/**/*.gif")
                .addResourceLocations("file:src/main/resources/static/", "classpath:/static/");
        
        log.info("添加静态资源映射: 图片资源 -> {}", fileUploadPath);
    }

    @Bean
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

}
